import Vue from 'vue'

Vue.filter('capacity',(value)=>{
	let val = value
	return (val / 1024).toFixed(1) + 'KB'
})

Vue.filter('untimestamp',(time,format="yyyy-MM-dd hh:mm:ss")=>{

    if(!time) return '-'
        
	let d = new Date(time*1000)
    let date = {
           "M+": d.getMonth() + 1,
           "d+": d.getDate(),
           "h+": d.getHours(),
           "m+": d.getMinutes(),
           "s+": d.getSeconds(),
           "q+": Math.floor((d.getMonth() + 3) / 3),
           "S+": d.getMilliseconds()
    };
    if (/(y+)/i.test(format)) {
           format = format.replace(RegExp.$1, (d.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (let k in date) {
       if (new RegExp("(" + k + ")").test(format)) {
              format = format.replace(RegExp.$1, RegExp.$1.length == 1
                     ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
       }
    }
	return format
})

Vue.filter('imgServer',(url,suffix="")=>{
    if(url)
        return url.indexOf("://")>=0 ? url + suffix : "http://img.donguayx.com/project/170905/" + url + suffix
    else
        return ""
})


Vue.filter('countDown',(timestamp)=>{
    let time = [0,0,0]

    timestamp = parseInt(timestamp)
    if(timestamp){
        time[0]   =  Math.floor(timestamp/3600)
        timestamp =  timestamp % 3600
        time[1]   =  Math.floor(timestamp/60)
        timestamp =  timestamp % 60
        time[2]   = timestamp
        //3600
    }
    time[0] = time[0] > 999 ?  999 : time[0]
    for(let i=0;i<time.length;i++){
        time[i] = time[i] < 10 ? `0${time[i]}` : `${time[i]}`
    }
    return `${time[0]}:${time[1]}:${time[2]}`
    //return time
})